<?php
namespace app\farm\model;

use app\BaseModel;
use think\facade\Db;

class LandOrderSeed extends BaseModel
{
    //定义表名
    protected $name = 'lbfarm_land_order_seed';


    protected $append = [

        'source',

        'total_weight',

        'send_tmpl_id'

    ];


    /**
     * @author chenniang
     * @DataTime: 2022-08-04 14:14
     * @功能说明:获取配送模版id
     */
    public function getSendTmplIdAttr($value,$data){

        if(!empty($data['seed_id'])){

            $seed_model = new Seed();

            $id = $seed_model->where(['id'=>$data['seed_id']])->value('send_tmpl_id');

            return $id;
        }

    }



    /**
     * @param $value
     * @param $data
     * @功能说明:总重量
     * @author chenniang
     * @DataTime: 2022-08-04 11:23
     */
    public function getTotalWeightAttr($value,$data){

        if(isset($data['output_value'])&&isset($data['area'])){

            return round($data['output_value']*$data['area'],2);
        }

    }


    /**
     * @author chenniang
     * @DataTime: 2022-02-14 10:11
     * @功能说明:溯源
     */
    public function getSourceAttr($value,$data){

        if(!empty($data['source_id'])){

            $source_model = new Source();

            $source = $source_model->dataInfo(['id'=>$data['source_id']]);

            return $source;

        }

    }




    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        $data['create_time'] = time();

        $res = $this->insert($data);

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);;

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-21 16:52
     * @功能说明:添加订单种子
     */
    public function orderSeedAdd($data,$order_id){

        $seed_model = new Seed();

        if(!empty($data)){

            foreach ($data as $v){

                $insert = [

                    'uniacid'      => $v['uniacid'],

                    'order_id'     => $order_id,

                    'title'        => $v['title'],

                    'imgs'         => !empty($v['imgs'][0])?$v['imgs'][0]:'',

                    'output_value' => $v['output_value'],

                    'month'        => implode(',',$v['month']),

                    'area'         => $v['area'],

                    'grow_cycle'   => $v['grow_cycle'],

                    'picking_cycle'=> $v['picking_cycle'],

                    'seed_cycle'   => $v['seed_cycle'],

                    'seed_price'   => $v['seed_price'],

                    'num'          => $v['num'],

                    'seed_id'      => $v['id'],

                    'source_id'    => $v['source_id'],

                    'total_price'  => round($v['num']*$v['seed_price'],2),

                ];

                $this->insert($insert);

                $seed_model->updateSaleNum($v['id'],$v['num']);

            }

        }

        return true;

    }


    /**
     * @author chenniang
     * @DataTime: 2022-08-04 14:00
     * @功能说明:获取配送飞
     */
    public function getSendPrice($address_id,$order_id,$times=0){

        if($times>0){

            return 0;
        }

        $list = $this->where(['order_id'=>$order_id])->field('*,num as goods_num')->select()->toArray();

        $config_model = new Config();

        $price = $config_model->getTotalSendPrice($address_id,$list,2);

        return $price;
    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page=10){

        $data = $this->where($dis)->order('top desc,id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-16 15:39
     * @功能说明:毁掉
     */
    public function orderResult($order_code,$transaction_id){

        $order = $this->dataInfo(['order_code'=>$order_code]);

        if(!empty($order)&&$order['pay_type']==1){

            $update = [

                'pay_time' => time(),

                'pay_type' => 2,

                'transaction_id' => $transaction_id
            ];

            $this->dataUpdate(['id'=>$order['id']],$update);

        }

        return true;

    }


    /**
     * @author chenniang
     * @DataTime: 2022-02-11 16:02
     * @功能说明:订单种子
     */
    public function orderSeed($order_id){

        $data  = $this->where(['order_id'=>$order_id])->select()->toArray();

        return $data;

    }






}